<?php
namespace app\common\service\api\service;
use app\common\service\api\interfaces\ResourceApi;
use app\common\exception\TokenException;
use app\common\exception\LogicException;
use think\db\exception\DataNotFoundException;
class ResourceApiService implements ResourceApi{
    public function getResourceListPage(){
        $where[] = ['resource_id', '>', 0];
        return app('resource_ability')->getListPage($where);
    }
    public function saveResourceAdd(){}
    public function getResourceRow(){
        $requestData = input();
        $where[] = ['resource_id', '=', $requestData['resource_id']];
        return app('resource_ability')->getLine($where);
    }
    public function saveResourceEdit(){
        $this->getResourceRow();
        $requestData = input();
        return app('resource_ability')->saveEdit($requestData);
    }
    public function saveResourceDel(){
        $this->getResourceRow();
        $requestData = input();
        return app('resource_ability')->saveDel($requestData);
    }
    public function setResourceStatus(){
        $requestData = input();
        return app('resource_ability')->setResourceStatus($requestData);
    }
    public function cleanUpExpiredFiles(){
        $requestData = input();
        return app('resource_ability')->cleanUpExpiredFiles($requestData);
    }
    public function uploadImg(){
        $file = request()->file('img_file');
        if (!$file) {
            throw new LogicException(lang('UPLOAD_ERROR'));
        }
        // 验证文件格式及大小
        validate(['imgFile' => [
            'fileSize' => 1024*1024*2,
            'fileExt' => 'jpg,jpeg,png,bmp,gif',
            'fileMime' => 'image/jpeg,image/png,image/gif', //这个一定要加上，很重要我认为！
        ]])->check(['imgFile' => $file]);
        $fileData['file']=$file;
        $fileData['type']=1;
        return app('resource_ability')->uploadFile($fileData);
    }
    public function uploadFile(){
        $file = request()->file('file');
        if (!$file) {
            throw new LogicException(lang('UPLOAD_ERROR'));
        }
        // 验证
        validate(['file' => [
            'fileSize' => 1024*1024*20,
        ]])->check(['file' => $file]);
        $fileData['file']=$file;
        $fileData['type']=2;
        return app('resource_ability')->uploadFile($fileData);
    }
}